package util;

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 
 * <p>DesUtil</p>
 * <p>类的详细说明</p>
 * <p>Company: 北京先进数通信息技术股份公司</p> 
 * @author meigx
 * @version 1.0 2014年5月10日
 * <p>修改者姓名 修改内容说明</p>
 */
public class DesUtil {
	private static String  password = "123456";  
    private static String alias = "www.zlex.org";  
    private  static String certificatePath = "d:/zlex.cer";  
    private  static String keyStorePath = "d:/zlex.keystore";
	private final static String DES = "DES";
	
	public static void main(String[] args) throws Exception {
	     String data = "eyJoZWFkIjp7Im1ick9OIjoiMjIwMCIsImluaXRTeXNJcEFkZHIiOiIxNzIuMjIuMTMuMjAwIiwiY29ycHJPTiI6IjIyMDAxMzgyIiwiaW5pdFN5c01hY0FkZHIiOiIiLCJicmNoT04iOiIyMjAwMzAwNCIsInJlcVRzIjoiMjAyMDA2MjQxNzM4MjEuMDIxIiwic2lnblNuIjoiMjAzMTMwMjA0OSIsInJlcVNlciI6IjAwMDAwMDAwMDIyMDAyMDIwMDYyNDE3MzgyMTAwMjEzIn0sImJvZHkiOnsiY3JkbHNObyI6IjExMDAwMDE5OTAwMTA5MTExNiIsImNyZGxzVHlwIjoiMDIiLCJwaG9uZU5vIjoiMTM1MjQ2MTczMDkiLCJubSI6Iuael+S5nSJ9fQ==";
	     String key = "meixiangdaoba";
//	     String cryString=encrypt(data, key);
//	     System.out.println(cryString);
		System.out.println(DesUtil.decrypt(data,key));
//	     byte[] decrypt =CertificateCoder.encryptByPublicKey(data.getBytes(), certificatePath);
	     
//	     String ttString=new String(decrypt);
//	     System.out.println(ttString.getBytes().length+"  "+ttString);
	     //String pubSign =CertificateCoder.pubSign(decrypt,certificatePath);
	     /*
	     String sign = CertificateCoder.sign(decrypt,keyStorePath,alias,password);
	     
	     String totString=sign+cryString;	     
	     byte[] dd=totString.getBytes();
	     byte[] gg=new byte[178];
	     byte[] tt=new byte[dd.length-178];
	     System.out.println(sign.getBytes().length+"  "+dd.length);
	     for(int i=0;i<178;i++){
	    	 gg[i]=dd[i];
	     }
	     for(int i=178;i<dd.length;i++){
	    	 tt[i-178]=dd[i];
	     }
	     boolean status = CertificateCoder.verify(tt,new String(gg),certificatePath);  
	     boolean status1=CertificateCoder.prtVerify(decrypt, pubSign, keyStorePath, alias, password);
	     System.err.println("状态:\r" + status+"  "+status1);
	     decrypt(cryString, key);
	     System.out.println(decrypt(new String(tt), key));
	     */
//	     byte[] bytes=CertificateCoder.decryptByPrivateKey(decrypt, keyStorePath, alias, password);
	 }  
	 /**
	  * Description 根据键值进行加密
	  * @param data
	  * @param key  加密键byte数组
	  * @return
	  * @throws Exception
	  */
	 public static String encrypt(String data, String key) throws Exception {
	     byte[] bt = encrypt(data.getBytes("GBK"), key.getBytes());
	     String strs = new BASE64Encoder().encode(bt);
	     return strs;
	 }
	 /**
	  * Description 根据键值进行解密
	  * @param data
	  * @param key  加密键byte数组
	  * @return
	  * @throws IOException
	  * @throws Exception
	  */
	 public static String decrypt(String data, String key) throws IOException,
	         Exception {
	     if (data == null)
	         return null;
	     BASE64Decoder decoder = new BASE64Decoder();
	     byte[] buf = decoder.decodeBuffer(data);
	     byte[] bt = decrypt(buf,key.getBytes());
	     return new String(bt);
	 }
	 /**
	  * Description 根据键值进行加密
	  * @param data
	  * @param key  加密键byte数组
	  * @return
	  * @throws Exception
	  */
	 private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
	     // 生成一个可信任的随机数源
	     SecureRandom sr = new SecureRandom();
	     // 从原始密钥数据创建DESKeySpec对象
	     DESKeySpec dks = new DESKeySpec(key);
	     // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
	     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
	     SecretKey securekey = keyFactory.generateSecret(dks);
	     // Cipher对象实际完成加密操作
	     Cipher cipher = Cipher.getInstance(DES);
	     // 用密钥初始化Cipher对象
	     cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
	     return cipher.doFinal(data);
	 }
	 /**
	  * Description 根据键值进行解密
	  * @param data
	  * @param key  加密键byte数组
	  * @return
	  * @throws Exception
	  */
	 private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
	     // 生成一个可信任的随机数源
	     SecureRandom sr = new SecureRandom();
	     // 从原始密钥数据创建DESKeySpec对象
	     DESKeySpec dks = new DESKeySpec(key);
	     // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
	     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
	     SecretKey securekey = keyFactory.generateSecret(dks);
	     // Cipher对象实际完成解密操作
	     Cipher cipher = Cipher.getInstance(DES);
	     // 用密钥初始化Cipher对象
	     cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
	     return cipher.doFinal(data);
	    }

}
